from typing import List, Union
from fastapi import APIRouter, Depends, Response
from sqlalchemy.orm import Session
from datetime import datetime
from db import get_db, crud
from schemas.customer import CustomerModel
from schemas.shipping_address import CreateShippingAddressRequest, ShippingAddressModel, \
    UpdateShippingAddressRequest, SetDefaultAddressRequest
from .customer import verify_customer_token

router = APIRouter(prefix="/customer/shipping-address", tags=["收货地址"])


@router.post("", response_model=ShippingAddressModel, response_model_exclude_none=True)
def create_shipping_address(data: CreateShippingAddressRequest,
                            customer: CustomerModel = Depends(verify_customer_token),
                            db: Session = Depends(get_db)):
    data.created_on = datetime.now()
    data.customer_id = customer.id
    count = crud.ShippingAddress.count_by_customer_id(db, customer.id)
    if count == 0:
        data.is_default = True

    return crud.ShippingAddress.create(db, data)


@router.get("/detail/{address_id}",
            response_model=ShippingAddressModel,
            response_model_exclude_none=True,
            dependencies=[Depends(verify_customer_token)])
def get_address_detail(address_id: int, db: Session = Depends(get_db)):
    return crud.ShippingAddress.get(db, address_id)


@router.get("/all", response_model=List[ShippingAddressModel], response_model_exclude_none=True)
def get_shipping_address(customer: CustomerModel = Depends(verify_customer_token),
                         db: Session = Depends(get_db)):
    return crud.ShippingAddress.get_all_by_customer_id(db, customer.id)


@router.put("", dependencies=[Depends(verify_customer_token)])
def update_shipping_address(data: UpdateShippingAddressRequest,
                            db: Session = Depends(get_db)):
    data.updated_on = datetime.now()
    crud.ShippingAddress.update(db, data)
    return Response()


@router.put("/default")
def set_default_address(data: SetDefaultAddressRequest,
                        customer: CustomerModel = Depends(verify_customer_token),
                        db: Session = Depends(get_db)):
    crud.ShippingAddress.set_default_address(db, customer.id, data.id)
    return Response()


@router.get("/default", response_model=Union[ShippingAddressModel, None], response_model_exclude_none=True)
def get_default_address(
        customer: CustomerModel = Depends(verify_customer_token),
        db: Session = Depends(get_db)):
    return crud.ShippingAddress.get_default_address(db, customer.id)


@router.delete("/{address_id}")
def delete_address(address_id: int,
                   customer: CustomerModel = Depends(verify_customer_token),
                   db: Session = Depends(get_db)):
    crud.ShippingAddress.delete(db, customer.id, address_id)
    address_list = crud.ShippingAddress.get_all_by_customer_id(db, customer.id)
    if len(address_list) > 0:
        crud.ShippingAddress.set_default_address(db, customer.id, address_list[0].id)
    return Response()
